﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Ultilities.Ultility;
    using ManagerClinic.Modules;
    using System.Linq;

    public partial class frmDmHangSanXuat : DevExpress.XtraEditors.XtraForm
    {

        GridCheckMarksSelection gridCheckMarksSelection = null;
        DmQuocGiaBusiness dmQuocGiaBusiness = new DmQuocGiaBusiness();
        DmHangSanXuatBusiness dmHangSanXuatBusiness = new DmHangSanXuatBusiness();
        IList<DmHangSanXuatEntity> lstDmHangSanXuatEntity = null;

        public frmDmHangSanXuat()
        {
            InitializeComponent();
            gridCheckMarksSelection = new GridCheckMarksSelection(gvHangsx);
            LoadQuocGia();
        }

        private void LoadQuocGia()
        {
            var lstQuocGiaEntity = dmQuocGiaBusiness.GetDmQuocGiaList();
            lstQuocGiaEntity.Insert(0, new DmQuocGiaEntity() { MaQuocGia = ConstantManagerClinic.sValueMember, TenQuocGia = ConstantManagerClinic.sDisplayMember });

            lkQuocGia.Properties.DataSource = lstQuocGiaEntity;
            lkQuocGia.Properties.DisplayMember = "TenQuocGia";
            lkQuocGia.Properties.ValueMember = "MaQuocGia";
            lkQuocGia.EditValue = ConstantManagerClinic.sValueMember;
        }

        private void LoadDataGrid(object sender, EventArgs e)
        {
            lstDmHangSanXuatEntity = dmHangSanXuatBusiness.GetDmHangSanXuatList();
            if (lstDmHangSanXuatEntity.Count > 0 && lkQuocGia.EditValue.ToString() != ConstantManagerClinic.sValueMember)
            {
                lstDmHangSanXuatEntity = lstDmHangSanXuatEntity.Where(n => n.MaQuocGia == lkQuocGia.EditValue.ToString()).ToList();
            }
            grdHangsx.DataSource = lstDmHangSanXuatEntity;
        }

        DmHangSanXuatEntity GetRow()
        {
            if (gvHangsx.RowCount > 0)
                return (DmHangSanXuatEntity)gvHangsx.GetFocusedRow();
            return null;
        }

        private void frmDMHangsanxuat_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadDataGrid(sender, e);
        }

        private void gvHangsx_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmHangSx((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.dmHangSxEvent += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            DmHangSanXuatEntity dmHangSanXuatEntity = GetRow();
            if (dmHangSanXuatEntity == null) return;
            var frm = new frmCapNhatDmHangSx((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmHangSanXuatEntity);
            frm.dmHangSxEvent += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void grdHangsx_DoubleClick(object sender, EventArgs e)
        {
            if (btnSua.Enabled == false) return;
            btnSua_Click(sender, e);
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }

            foreach (DmHangSanXuatEntity item in deleteObjects)
            {
                dmHangSanXuatBusiness.DeleteDmHangSanXuat(item.MaHang);
            }
            gridCheckMarksSelection.ClearSelection();
            LoadDataGrid(sender, e);
        }

        private void lkQuocGia_EditValueChanged(object sender, EventArgs e)
        {
            LoadDataGrid(sender, e);
        }

    }
}